﻿#include <boost/algorithm/string.hpp>
#include "BandBQCheck.h"
#include <BPAFilesIO.h>
#include <BaseCommon.h>

using namespace JGG;

BandBQcheck::BandBQcheck(BPA::BPAFilesIO::DatParse* datParse, const char* database, int caseid)
{
    this->datParse = datParse;
    this->database = database;
    this->caseid = caseid;

    inst = new BPA::BPAFilesIO::GetDatFromDb(datParse, database, caseid);
}
BandBQcheck::~BandBQcheck()
{
    if (inst)
    {
        delete inst;
        inst = nullptr;
    }
}
void BandBQcheck::BandBQParCheck()
{
	BPA::BPAFilesIO::GetInitConfigFromDb *ReadIni = new BPA::BPAFilesIO::GetInitConfigFromDb(database);
	vector <double> GeneratorVoltage = ReadIni->GetGenVoltage();
	int GeneratorVoltageNum = GeneratorVoltage.size();
	BUSVector BusPvList = inst->GetBusPVList();
	for (int i = 0; i < BusPvList.size(); i++)
	{
		if(BusPvList[i].BusType == "BQ")
		{
			int Flag = 1;
			double *error = new double[GeneratorVoltageNum];
			for (int m=0;m<GeneratorVoltageNum;m++)
			{
				error[m] = fabs(BusPvList[i].BusVol - GeneratorVoltage[m])/GeneratorVoltage[m];
				if (error[m] == 0)
				{
					Flag = 0;
					break;
				}
			}
			if (Flag == 0)
			{
				continue;
			}
            double ErrorTmp;
			for (int j = 0; j < GeneratorVoltageNum; j++)
			{
				if (j == 0)
				{
					ErrorTmp = error[0];
				}
				else
				{
					if (error[j]<ErrorTmp)
					{
						ErrorTmp = error[j];
					}
				}
			}
            double BusVolStandard = 0.3;
			if (ErrorTmp> BusVolStandard)
			{
				BusPvList[i].BusProblem = "BQ发电机节点设置可能不合理，机端电压过高";
				BQProList.push_back(BusPvList[i]);
			}
			delete[] error;
		}
	}
}

void BandBQcheck::BadBus2Db()
{
	SqliteHelperDataBase db;
	SqliteHelperBuffer bufSQL;
	db.open(database);
	//写入前清空数据
	char buf[800];
	sprintf_s(buf, "delete from  BusProblemList where CaseID = (%d);", caseid);
	db.execDML(buf);
	db.beginTransaction();
	for (int i = 0; i<BQProList.size(); i++)
	{
		std::memset(buf, 0, sizeof(buf));
		sprintf_s(buf, "insert into BusProblemList (CaseID,BusID,BusType,BusName,BusVol,BusPro) values (%d,%d,'%s','%s',%f,'%s');", caseid, BQProList[i].BusNum, BQProList[i].BusType.c_str(), BQProList[i].BusName.c_str(), BQProList[i].BusVol, BQProList[i].BusProblem.c_str());
		db.execDML(buf);
	}
	db.commitTransaction();
	db.close();
	//BProList.clear();
	BQProList.clear();
}

void BandBQcheck::BpaFindBadPowerFactor()
{
	SqliteHelperDataBase db;
	SqliteHelperBuffer bufSQL;
	db.open(database);
	//string FileName;
	
	//bufSQL.format(" select rowid from FileList where  FileName = (%Q);",FileName.c_str() );
	//int CaseIDTmp = db.execScalar(bufSQL);		//获取当前的文件编号
	BUSVector BusList = inst->GetBusList();
	int BusNum = BusList.size();
	double Factor,Qshunt;
	SqliteHelperQuery query_cfg = db.execQuery("select * from UIConfig;");
	double PowerFactor = query_cfg.getFloatField(10);		//功率因数值
	char buf[500];
	sprintf_s(buf, "delete from  MinPFBusListBadFactor where CaseID = (%d);", caseid);//母线相关信息导入数据库前清空
	db.execDML(buf);
	db.beginTransaction();
	for(int i=0;i<BusNum;i++)
	{
		if(BusList[i].Pload !=0 && BusList[i].Qload != 0 && BusList[i].Pload !=999999 && BusList[i].Qload != 999999)
		{
			if (BusList[i].Qshunt == 999999)
				Qshunt = 0;
			else
				Qshunt = BusList[i].Qshunt;
			Factor = pow(BusList[i].Pload,2) + pow(BusList[i].Qload - Qshunt,2);
			Factor = fabs(BusList[i].Pload)/pow(Factor,0.5);
			if(Factor < PowerFactor)
			{
                char buf[1024] = {0};
				sprintf_s(buf, "insert into MinPFBusListBadFactor(CaseID,BusID,VolBase,ZoneName,PLoad,QLoad,Qshunt,PowerFactor) values (%d,%d,%f,'%s',%f,%f,%f,%f);",
					caseid, BusList[i].BusNum, BusList[i].BusVol, BusList[i].BusZone.c_str(), BusList[i].Pload, BusList[i].Qload, Qshunt, Factor);
				db.execDML(buf);
			}
		}
	} 
	db.commitTransaction();
	db.close();
}
//string BandBQcheck::Unicode2Utf8(const std::wstring& widestring)  
//{  
//
//	int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);  
//	if (utf8size == 0)  
//	{  
//		throw std::exception("Error in conversion.");  
//	}  
//
//	std::vector<char> resultstring(utf8size);  
//
//	int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);  
//
//	if (convresult != utf8size)  
//	{  
//		throw std::exception("La falla!");  
//	}  
//
//	return std::string(&resultstring[0]);  
//}  
//bool BandBQcheck::StringToWString(const std::string &str,std::wstring &wstr)
//{
//	int nLen = (int)str.length();    
//	wstr.resize(nLen,L' ');
//
//	int nResult = MultiByteToWideChar(CP_ACP,0,(LPCSTR)str.c_str(),nLen,(LPWSTR)wstr.c_str(),nLen);
//
//	if (nResult == 0)
//	{
//		return FALSE;
//	}
//
//	return TRUE;
//
//}
//wstring BandBQcheck::str2Wstr(string str)
//{
//	wstring Wstr;
//	StringToWString(str,Wstr);
//	return Wstr;
//
//}
